import { getSizeNos } from '@/api/box/workbench'

export const tagMixin = {
  data () {
    return {
      showBottom: false,
      isRefreshBool: false,
      paramTree: [],
      initialFilterArray: [],
      pageSize: 20,
      current: 1,
      total: 0,
      pages: 0,
      currentTotal: 0,
      goodsList: [],
      loading: true,
      loadingMore: true,
      showLoadingMore: false
    }
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll, true)
  },
  deactivated () {
    window.removeEventListener('scroll', this.handleScroll, true)
  },
  destroyed () {
    window.removeEventListener('scroll', this.handleScroll, true)
  },
  computed: {
    bandsName (arr, list) {
      return (arr, list) => {
        if (arr && arr.length > 0) {
          let newList = []
          list.forEach(item => {
            arr.forEach(child => {
              if (child == item.id) {
                newList.push(item.attribname)
              }
            })
          })
          return newList.join('，')
        }
      }
    }
  },
  methods: {
    filterOption (input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    // 获取筛选树
    getFilterTree () {
      return getSizeNos().then(res => {
        if (res.code == 0) {
          this.paramTree = res.data.slice(0, 4)
          // this.paramTree = JSON.parse(JSON.stringify(res.data))
          for (let item of this.paramTree) {
            Object.assign(item, {
              isMuilt: true,
              isCustom: false,
              checkList: [],
              radioValue: null
            })
            if (item.name == '类目') {
              item.toggleNum = 5
              item.toggle = false
            }
          }
          // 预处理Tree
          this.handleInitalTree(this.paramTree)
          this.initialFilterArray = this.paramTree.map(e => {
            return {
              checkValue: [],
              codes: [],
              names: [],
              title: e.name
            }
          })
        }
      })
    },
    handleInitalTree (data) {
      for (let item of data) {
        if (item.children != undefined && item.children != null) {
          let arr = item.children.filter(e => e.children != undefined && e.children != null)
          if (item.children && arr.length > 0) {
            this.handleInitalTree(item.children)
          } else {
            Object.assign(item, {
              checkValue: [],
              checkList: [],
              indeterminate: false,
              checkAll: false
            })
          }
        } else {
          Object.assign(item, {
            checkValue: [],
            checkList: [],
            indeterminate: false,
            checkAll: false
          })
        }
      }
    },
    // 滚动加载
    handleScroll () {
      //变量scrollTop是滚动条滚动时，距离顶部的距离
      var scrollTop = document.documentElement.scrollTop || document.body.scrollTop
      //变量windowHeight是可视区的高度
      var windowHeight = document.documentElement.clientHeight || document.body.clientHeight
      //变量scrollHeight是滚动条的总高度
      var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight

      //滚动条到底部的条件
      if (scrollTop + windowHeight + 20 >= scrollHeight && this.isRefreshBool) {
        console.log('距顶部' + scrollTop + '可视区高度' + windowHeight + '滚动条总高度' + scrollHeight)
        this.isRefreshBool = false
        this.showBottom = true
        if (this.current == this.pages) {
          this.loadingMore = false
        } else {
          this.loadingMore = true
          let that = this
          setTimeout(function () {
            that.onLoadMore()
          }, 300)

        }
      }
    },
    // 加载更多
    onLoadMore () {
      console.log('------onLoadMore-----')
      this.loadingMore = true
      this.current += 1
      const param = JSON.parse(JSON.stringify(this.queryParam))
      this.fetchList(param)
    },
    // 编辑商品下一个到当前页的最后一个时，加载下一页
    loadingPage (value) {
      if (value) {
        this.onLoadMore()
      }
    },
    // 筛选条件
    updateParams (data) {
      console.log(data)
      let paramsArr = JSON.parse(JSON.stringify(data))
      // console.log('updateParams:', paramsArr)
      if (paramsArr.length > 0) {
        let brandItem = paramsArr.filter(e => e.title == '品牌')
        this.queryParam.brandIds = brandItem[0].codes

        let seasonItem = paramsArr.filter(e => e.title == '季节')
        this.queryParam.seasonIds = seasonItem[0].codes

        let yearItem = paramsArr.filter(e => e.title == '年份')
        this.queryParam.years = []
        if (yearItem[0].checkValue.length > 0) {
          for (let child of yearItem[0].checkValue) {
            if (child.name == '其他年份') {
              let arr = child.code.map(e => e.code)
              this.queryParam.years = [...this.queryParam.years, ...arr]
            } else {
              this.queryParam.years.push(child.code)
            }
          }
        }

        let smallCategory = paramsArr.filter(e => e.title == '类目')
        this.queryParam.smallCategoryIds = smallCategory[0].codes
        console.log(this.queryParam)
        this.searchList()
      }
    },
    updateSearchText (text) {
      this.queryParam.name = text
      this.searchList()
    }
  },
}